<?php

/**
 * news actions.
 *
 * @package    cpfaizenay
 * @subpackage news
 * @author     Manu
 * @version    1.0
 */
class newsActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request) {
      $slug = $request->getParameter('title_slug');
      $this->currentNews = Doctrine::getTable('News')->getCurrentNews($slug)->fetchOne();
      $this->forward404Unless($this->currentNews);

      isicsBreadcrumbs::getInstance()->addItem($this->currentNews->getTitle(), '#');

      $response = $this->getResponse();
      Utils::setSlogTitle($this->currentNews['title'], $response);
      $response->addMeta('description', Utils::slugDescription($this->currentNews['text']));

      // facebook meta
      $response->addMeta('og:title', $this->currentNews->getTitle());
      $response->addMeta('og:type', 'article');
      $response->addMeta('og:description', Utils::slugDescription($this->currentNews['text']));
      if($this->currentNews['sfAsset'][ 'filename' ]){
        $response->addMeta('og:image', sfConfig::get('app_homePage_uri') . "/" . $this->currentNews['sfAsset'][ 'Folder' ][ 'relative_path' ] . "/thumbnail/small_" . $this->currentNews['sfAsset'][ 'filename' ]);
      }
  }

  public function executeAllNews(sfWebRequest $request) {
      $this->pager = new sfDoctrinePager(
        'News',
        sfConfig::get('app_news_max_all_news')
      );
      $this->pager->setQuery(Doctrine::getTable('News')->getNews());
      $this->pager->setPage($request->getParameter('page', 1));
      $this->pager->init();
      $this->news_list = $this->pager;

      isicsBreadcrumbs::getInstance()->addItem('Actualités de CPF Aizenay', '#');

      $response = $this->getResponse();
      Utils::setSlogTitle('Actualités de CPF Aizenay', $response);
  }

  // Ajax load
  public function executeLoadKindNews(sfWebRequest $request) {
      if ($request->isXmlHttpRequest()){
          $kindNews = $request->getParameter('kindNews');

          $news_list = Doctrine::getTable('News')->getNews(null, $kindNews, sfConfig::get('app_news_max_news_on_homepage'))->execute(array(),Doctrine::HYDRATE_ARRAY);

          return $this->renderPartial('news/homeShow', array('news_list' => $news_list, 'kindNews' => $kindNews));
      }
  }


}